<template>
	<view>
		<u-row gutter="16" justify="center" style="background: #1E88E5;">
			<u-col span="11">
				<u-search shape="square" bg-color="#fff" placeholder="输入访客人员姓名关键字" :action-style="actionStyle" v-model="keyword" style="margin: 48rpx 0 30rpx 0;" @search="search" @custom="search"></u-search>
			</u-col>
		</u-row>
		<scroll-view class="scroll" scroll-y="true">
			<view class="box" v-for="(item, index) in list" :key="index" :name="item.id">
				<u-row gutter="16">
					<u-tag v-if="item.type==5" text="政" border-color="#FFECB2" bg-color="#FFECB2" color="#FEB500"></u-tag>
					<u-tag v-if="item.type==6" text="驻" border-color="#DAEDFF" bg-color="#DAEDFF" color="#1976D2"></u-tag>
					<u-tag v-if="item.type==7" text="面" border-color="#AFECB2" bg-color="#AFECB2" color="#1EB500"></u-tag>
					<u-tag v-if="item.type==8" text="他" border-color="#DAEDFF" bg-color="#DAEDFF" color="#1976D2"></u-tag>
					<view style="display: flex;justify-content: space-between;width: 550rpx;">
						<view class="head-title">{{item.visitor}}</view>
						<view v-if="item.status>10 && item.status!=50" class="head-title" style="color: green;">已接待</view>
						<view v-if="item.status==10" class="head-title" style="color: red;">未接待</view>
						<view v-if="item.status==0" class="head-title" style="color: red;">审批中</view>
						<view v-if="item.status==-1" class="head-title" style="color: red;">请等待</view>
						<view v-if="item.status==50" class="head-title" style="color: grey;">已结束</view>
					</view>
				</u-row>
				<u-row gutter="16" style="margin-top: 25rpx;">
					<u-col span="12">
						<view class="apply-text">手机号码：{{item.vphone}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text">拜访事由：{{item.vreason}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text">来访时间：{{item.visitTime}}</view>
					</u-col>
				</u-row>
				<u-row gutter="16" style="margin-top: 20rpx;">
					<u-col span="12">
						<u-button plain @click="goDetail(item)" style="height: 60rpx;">{{item.status==10?'前往接待':'查看详情'}}</u-button>
					</u-col>
				</u-row>
			</view>
			<view style="height: 50rpx;"></view>
		</scroll-view>
		<!-- <u-row class="foot" gutter="32">
		    <u-col span="3">
		        <u-button :disabled="currentPage === 1" @click="goToPage(currentPage - 1)">上一页</u-button>
		    </u-col>
			<u-col span="3">{{currentPage}}/{{totalPages}}</u-col>
			<u-col span="3">5条/页</u-col>
		    <u-col span="3">
		        <u-button :disabled="currentPage === totalPages" @click="goToPage(currentPage + 1)">下一页</u-button>
		    </u-col>
		</u-row> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				carNum: 0,
				actionStyle: {
					color: '#fff'
				},
				keyword: '',
				list: [],
				allList: [],
				currentPage: 1,
				totalPages: 0,
				pageSize: 5000000 // 每页显示的条目数
			}
		},
		onLoad(options) {
			this.getApplicants(options.staff)
			// this.getVisTypeEnterCount()
		},
		methods: {
			goToPage(page) {
			    if (page < 1 || page > this.totalPages) return; // 确保页码在有效范围内
			    this.currentPage = page;
			    this.getApplicants(); // 重新请求数据
			},
			getVisTypeEnterCount() {
				this.$u.get('/visitlogextend/vistypeentercount').then(res => {
					if(res.code==200){
						const found = res.result.find(item => item.type == 2)
						if (found) {
							this.carNum =  found.count
						}
					}else{
						this.$u.toast('获取车辆数失败',res.message)
					}	
				}).catch(err => {
					this.$u.toast('获取车辆数异常',err)
				})
			},
			goDetail(item) {
				if(item.status==10) {
					uni.navigateTo({
						url: '/pages/my/askCode?content='+item.id+','+item.status+','+item.type
					});
				}else{
					uni.navigateTo({
						url: '/pages/detail/detail?id='+item.id
					});
				}
			},
			reject() {
				if(this.ids.length==0){
					return this.$u.toast('至少勾选一条')	
				}
				this.$u.post('/visitlogextend/batch',{status:20,ids:this.ids}).then(res => {
					if(res.code==200){
						this.getApplicants()
						this.$u.toast('拒绝成功')
					}else{
						this.$u.toast(res.message)
					}	
				}).catch(err => {
					this.$u.toast(err)
				})
			},
			agree() {
				if(this.ids.length==0){
					return this.$u.toast('至少勾选一条')	
				}
				this.$u.post('/visitlogextend/batch',{status:10,ids:this.ids}).then(res => {
					if(res.code==200){
						this.getApplicants()
						this.$u.toast('同意成功')
					}else{
						this.$u.toast(res.message)
					}	
				}).catch(err => {
					this.$u.toast(err)
				})
			},
			getApplicants(staff) {
				const currentDate = new Date();   // 获取当前日期
				const startOfDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).toISOString();  // 设置为当前日期的 00:00:00
						
				this.$u.post('/visitlog/page',{staff,page:this.currentPage,pageSize:this.pageSize}).then(res => {
					if(res.code==200){
						this.checkedAll = false
						res.result.items.map(val => {
							val.checked = false
						})
						this.list = res.result.items.filter(item => item.type>4)
						this.allList = res.result.items.filter(item => item.type>4)
						
						this.totalPages = Math.ceil(res.result.total / this.pageSize); // 更新总页数
					}else{
						this.$u.toast('请求失败',res.message)
					}	
				}).catch(err => {
					this.$u.toast('请求异常',err)
				})
			},
			search() {
				this.list = this.allList.filter(item => item.visitor.includes(this.keyword))
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll {
		height: calc(100vh - var(--window-top) - var(--window-bottom) - 242rpx);
		width: 100%;
	}
	
	.box {
		width: 690rpx;
		background: #fff;
		margin: auto;
		margin-top: 30rpx;
		padding: 10rpx;
		border-radius: 12rpx;
		
		.u-tag {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 72rpx;
			height: 72rpx;
			border-radius: 100%;
			font-size: 40rpx;
			font-family: PingFang SC;
			font-weight: bold;
		}
	}
	
	.head-title {
		margin-left: 20rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #000000;
	}
	
	.apply-text {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #434343;
		margin: 8rpx;
	}
	
	.foot {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 112rpx;
		border-top: 1rpx solid #F5F5F5;
		background: #fff;
	}
</style>
